package com;

import java.util.*;

public class Test {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student("张三",30,"男"));
        list.add(new Student("张三",19,"女"));
        list.add(new Student("张三",35,"男"));
        list.add(new Student("张三",25,"女"));
        list.add(new Student("张三",20,"男"));
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if(o1.getAge()>o2.getAge()){
                    return 1;
                }else if(o1.getAge()<o2.getAge()){
                    return -1;
                }else {
                    return 0;
                }
            }
        });
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        System.out.println("年龄为25岁的学员在下标为"+a(list, new Student("张三",25,"女"), 0, list.size() - 1)+"的位置");
    }
    public static int  a(List<? extends Comparable> list , Comparable k ,int a ,int b){
        while(a<=b){
            int c=(a+b)/2;
            Comparable t=list.get(c);
            if (k.compareTo(t)<0){
                b=c-1;
            }else if (k.compareTo(t)>0){
                a=c+1;
            }else{
                return c;
            }
        }
        return -1;
    }
}
